package cn.com.fy.baidu.tanslate;

import java.io.UnsupportedEncodingException;

import com.alibaba.fastjson.JSONObject;

public class TestTranlate {

	private static final String APP_ID = "20180324000139607";
	private static final String SECURITY_KEY = "rxXnGlFjSSNUnxGnsqlU";

	public static void main(String[] args) throws UnsupportedEncodingException {
		TransApi api = new TransApi(APP_ID, SECURITY_KEY);

		String query = "铜陵市";
		String translate = api.getTransResult(query, "auto", "est");
		JSONObject resoult = JSONObject.parseObject(translate);
		String resoutl1 = resoult.getString("trans_result");
		if (resoutl1.isEmpty())
			return;
		resoutl1 = resoutl1.replace("[", "");
		resoutl1 = resoutl1.replace("]", "");
		JSONObject resoult2 = JSONObject.parseObject(resoutl1);
		String translate1 = (String) resoult2.get("dst");
		System.out.println(translate1);
	}

	public String getTranslate(String query, String to) throws UnsupportedEncodingException {

		String translate1 = "";
		try {
			TransApi api = new TransApi(APP_ID, SECURITY_KEY);
			String translate = api.getTransResult(query, "zh", to);
			JSONObject resoult = JSONObject.parseObject(translate);
			String resoutl1 = resoult.getString("trans_result");
			resoutl1 = resoutl1.replace("[", "");
			resoutl1 = resoutl1.replace("]", "");
			JSONObject resoult2 = JSONObject.parseObject(resoutl1);
			translate1 = (String) resoult2.get("dst");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println("睡眠开始");
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			System.out.println("睡眠结束");
			getTranslate(query, to);
		}
		return translate1;
	};
}
